package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityMyVisitVideoBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterMyVisitVideo
import com.gexiaobao.pigeon.ui.fragment.mine.pigeon.activity.ActivityVideoPlayer
import com.gexiaobao.pigeon.viewmodel.OrgDetailViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/6/14 15:46
 * @Describe    : 探视视频（我的）
 */
class ActivityMyVisitVideo : BaseActivity<OrgDetailViewModel, ActivityMyVisitVideoBinding>() {

    private val mAdapter: AdapterMyVisitVideo by lazy { AdapterMyVisitVideo(arrayListOf()) }

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>
    private var ringId = ""
    private var raceId = 0

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.visitation_video)

        raceId = intent.getIntExtra("raceId", 0)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 10f)))
            it.initFooter {
                //触发加载更多时请求数据
                mViewModel.getMyVisitVideo(false, "", ringId, raceId)
            }
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            //点击重试时触发的操作
            searchResultData(true)
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            searchResultData(true)
        }

        searchResultData(true)

        mDatabind.includeSearch.editQuery.addTextChangedListener {
            mDatabind.includeSearch.ivClose.isVisible = it.toString().isNotEmpty()
        }

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    searchResultData(true)
                    hideSoftKeyboard(this)
                }

                else -> {}
            }
            false
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity<ActivityVideoPlayer>(
                "videoUrl" to mAdapter.data[position].path,
                "title" to mAdapter.data[position].ringId,
                "type" to "",
            )
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.getVisitVideoResult.observe(this) {
            loadListData(it, mAdapter, loadsir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.includeBar.ivBack,
            mDatabind.includeSearch.tvSearch,
            mDatabind.includeSearch.ivClose,
        ) {
            when (it) {
                mDatabind.includeSearch.ivClose -> {
                    mDatabind.includeSearch.editQuery.setText("")
                    searchResultData(true)
                }

                mDatabind.includeSearch.tvSearch -> {
                    searchResultData(true)
                }

                mDatabind.includeBar.ivBack -> {
                    finish()
                }
            }
        }
    }

    private fun searchResultData(b: Boolean) {
        ringId = mDatabind.includeSearch.editQuery.text.toString().trim()
        onRefresh(b)
    }

    private fun onRefresh(refresh: Boolean) {
        loadsir.showLoading()
        mViewModel.getMyVisitVideo(refresh, "", ringId, raceId)
    }
}